import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { Barcode, DictApi } from '#/api';

import { reactive } from 'vue';

import { DictResp } from '#/api';

const status = reactive<DictApi.SelectOption[]>([]);
const type = reactive<DictApi.SelectOption[]>([]);
const formart = reactive<DictApi.SelectOption[]>([]);

DictResp(['mes_barcode_type', 'mes_barcode_formart', 'sys_yes_no']).then(
  (res) => {
    if (res.mes_barcode_type) {
      type.push(...res.mes_barcode_type);
    }
    if (res.mes_barcode_formart) {
      formart.push(...res.mes_barcode_formart);
    }
    if (res.sys_yes_no) {
      status.push(...res.sys_yes_no);
    }
  },
);

export const sys_yes_no = status;
export const mes_barcode_type = type;
export const mes_barcode_formart = formart;

/**
 * 获取条码设置表单字段
 */
export function useSchema(): VbenFormSchema[] {
  return [];
}
/**
 * 获取表格列配置
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  _onActionClick?: OnActionClickFn<Barcode.Row>,
): VxeTableGridOptions<Barcode.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
      width: 50,
    },
    {
      field: 'barcodeUrl',
      fixed: 'left',
      title: '条码',
      cellRender: { name: 'CellImage' },
    },

    {
      field: 'barcodeFormart',
      title: '条码格式',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_barcode_formart.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'barcodeType',
      title: '条码类型',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_barcode_type.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'barcodeContent',
      title: '条码内容',
    },
    {
      field: 'bussinessCode',
      title: '业务编码',
    },
    {
      field: 'bussinessName',
      title: '业务名称',
    },
    {
      field: 'enableFlag',
      title: '是否生效',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          sys_yes_no.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
      cellRender: { name: 'CellTag' },
    },

    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
